package mythread.a10threadsage2;


public class MyRunnable implements Runnable {
    int ticket = 0;

    @Override
    public void run() {
        //1、循环
        while (true) {
            //2、同步代码块(同步方法)
            if (sell()) break;
        }
    }

    // 非静态的:this
    private synchronized boolean sell() {
        //3、判断共享数据是否到了末尾，如果到了末尾
        if (ticket == 100) {
            return true;
        } else {
            //休眠一会
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            //4、判断共享数据是否到了末尾，如果没有到末尾
            //卖票
            ticket++;
            System.out.println(Thread.currentThread().getName() + "卖出了第" + ticket + "张票");
        }
        return false;
    }
}
